package org.shoukaiseki.inittable.utils;


import org.shoukaiseki.inittable.domain.TableIndexInfo;

public final class TableIndexInfoBuilder {
    private boolean uniqueIndex = false;
    private String indexName;
    private String indexColumns;

    private TableIndexInfoBuilder() {
    }

    public static TableIndexInfoBuilder index(String indexName) {
        TableIndexInfoBuilder tableIndexInfoBuilder = new TableIndexInfoBuilder();
        tableIndexInfoBuilder.uniqueIndex = false;
        tableIndexInfoBuilder.indexName = indexName;
        return tableIndexInfoBuilder;
    }

    /**
     * 索引规范参考
     * https://blog.csdn.net/yanyuan_1118/article/details/119863596
     * @param indexName
     * @return
     * <pre>
     *     1、单值索引
     *     建议以 idx_ 为开头，字母全部小写。
     *     例如：alter table t1 add key idx_r1(r1)；
     *     2、组合索引
     *     建议以 dx_multi_ 开头，字母全部小写。
     *     例如：alter table t1 add key idx_multi_1(r1,r2,r3) ;
     *     3、唯一索引
     *     建议以 udx_ 为开头，字母全部小写；如果是多值唯一索引，则命名方式类似 udx_multi_1 等。
     *     例如：
     *     alter table t1 add unique key udx_r1(r1)；
     *      或者
     *     alter table t1 add key udx_multi_1(r1,r2,r3);
     *
     * </pre>
     */
    public static TableIndexInfoBuilder uniqueIndex(String indexName) {
        TableIndexInfoBuilder tableIndexInfoBuilder = new TableIndexInfoBuilder();
        tableIndexInfoBuilder.uniqueIndex = true;
        tableIndexInfoBuilder.indexName = indexName;
        return tableIndexInfoBuilder;
    }


    public TableIndexInfoBuilder indexColumns(String indexColumns) {
        this.indexColumns = indexColumns;
        return this;
    }

    public TableIndexInfo build() {
        TableIndexInfo tableIndexInfo = new TableIndexInfo();
        tableIndexInfo.setUniqueIndex(uniqueIndex);
        tableIndexInfo.setIndexName(indexName);
        tableIndexInfo.setIndexColumns(indexColumns);
        return tableIndexInfo;
    }
}
